define(['app'], function(app) {
	'use strict';
	//该页面不需要上下拉刷新
	var controller = function controller($scope, Issuediscount, GoPage, GoDialog, $timeout) {

		//每次进入页面，都重新加载
		//		$scope.$on('$ionicView.afterEnter', function() {
		//			$scope.init_data();
		//		}, false);

		//初始化数据
		$scope.init_data = function() {
			/*
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				lan = 20;
			}
			var all = window.innerHeight;
			$scope.myObj = {
				"height": (all - lan - 100) + "px"
			}
			*/
			//动态设置高度
			GoPage.init_top($scope);
			Issuediscount.get_issuediscount(1, function(data) {
				//console.log(data)
				if(data != null) {
					$scope.list = data.coupon;
					$scope.count = data.page.count; //总数
				} else {
					$scope.list = new Array();
				}
			})
		}
		$scope.init_data();

		//跳转后刷新页面  /*******慎用********/
		$scope.$on('$ionicView.beforeEnter', function() {
			///局部刷新，更新所需的字段
			$scope.init_data();
		});

		/***************选择商品上下拉刷新*******************/
		$scope.nPage = 1;
		$scope.isLoad = false; //可以加载
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Issuediscount.get_issuediscount(1, function(data) {
				$scope.nPage = 1;
				if(data != null) {
					$scope.list = data.coupon;
				} else {
					$scope.list = new Array();
				}
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Issuediscount.get_issuediscount($scope.nPage + 1, function(data) {
				if(data != null && data.coupon != null && data.coupon.length > 0) {
					$scope.nPage += 1;
					if($scope.list != undefined) {
						$scope.list = $scope.list.concat(data.coupon);
					} else {
						$scope.list = data.coupon;
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		/***************选择商品上下拉刷新*******************/

		//跳转页面
		$scope.onshow_adddiscount = function(id, type) {
			GoPage.go_states("adddiscount", {
				"id": id,
				"type": type
			});
		}
		/*//进入编辑页面
		$scope.showInfo = function(id) {
			GoPage.go_states("adddiscount", {
				"id": id
			});
		}

		//删除优惠券
		$scope.del = function(id) {
			Issuediscount.del(id, function(data) {
				if(data.success == true) {
					GoDialog.toast("优惠券删除成功！");
					$scope.init_data();
				} else {
					GoDialog.toast(data.msg);
				}
			});
		}*/
		//图片放大			
		$scope.openFileActSheet = function($event, index) {
			$event.stopPropagation(); //阻止事件冒泡
			GoDialog.showBigImg($scope.list[index].qrcode);
		}
		$scope.clickbtn = function(error, btn_id, id, type) {
			if(error) {
				GoDialog.alert("", error);
			} else {
				if(btn_id == 1) { //进入编辑页面
					GoPage.go_states("adddiscount", {
						"id": id,
						"type": type
					});
				}
				if(btn_id == 2) { //删除优惠券
					GoDialog.confirm("提示", "确认要删除该优惠券么？", function() {
						Issuediscount.del(id, function(data) {
							if(data.success == true) {
								GoDialog.toast("优惠券删除成功！");
								$scope.init_data();
							} else {
								GoDialog.toast(data.msg);
							}
						})
					})

				}

			}
		}

	}

	controller.$inject = ['$scope', 'Issuediscount', 'GoPage', 'GoDialog', '$timeout'];

	return app.registerController("issuediscountctrl", controller);
});